<template>
    <div class="container">
        <h3>添加文章</h3>
        <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="文章标题">
            <el-input v-model="form.article_name"></el-input>
        </el-form-item>
        <el-form-item label="文章封面">
            <el-upload
            class="avatar-uploader"
            action="http://upload-cn-east-2.qiniup.com"
            :show-file-list="false"
            :on-success="dada"
            :data="data"
            >
            <img v-if="form.img" :src="form.img" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </el-form-item>
        <el-form-item label="文章内容">
                <el-input
                    type="textarea"
                    :autosize="{ minRows: 10, maxRows: 4}"
                    placeholder="请输入内容"
                    v-model="form.synopsis">
                </el-input>
        </el-form-item>

        </el-form>
        <el-button type="success" @click="addr">确认添加</el-button>
    </div>
</template>

<script>
import Axios from 'axios';
export default {
    name: '',
    data() {
        return {
            form:{
                article_name:'',
                img:'',
                synopsis:'',
            },
            data:{
                token:'',
            }
        };
    },
    props: {},

    components: {},

    created() {},

    mounted() {
        this.toeknget()
    },

    methods: {
        toeknget(){
            Axios.get('token/')
            .then(res => {
            console.log(res)
            if (res.data.code == 200){
                this.data.token = res.data.token
            }
        })
        .catch(err => {
          console.log(err)
        })
        },
        dada(response){
            console.log(response.key)
            this.form.img = 'http://rkm2dzt0t.bkt.clouddn.com/' + response.key
        },
        addr(){
            Axios.post('article/',this.form,{
                headers:{
                    'Authorization':localStorage.getItem('token') || ''
                }
            })
            .then(res => {
            console.log(res)
            if (res.data.code == 200){
                this.$notify({
              title: '成功',
              message: '发布成功',
              type: 'success'
            });
            this.$router.push('/about')
            }
            })
            .catch(err => {
            console.log(err)
            })
        }
    },

    computed: {
        
    },

    watch: {},

    directives: {},

    filters: {}
};
</script>

<style scoped>

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

</style>
